package com.lyw.forum.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lyw.forum.pojo.dataobject.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper extends BaseMapper<Message> {


    /**
     * 查询接收者的所有未读消息
     * @param receiveUserId 接收者id
     * @return  接收者的所有未读消息
     */
    @Select({
            "SELECT",
            "    u.id AS u_id,",
            "    u.avatar_url AS u_avatar_url,",
            "    u.nickname AS u_nickname,",
            "    m.id,",
            "    m.post_user_id,",
            "    m.receive_user_id,",
            "    m.content,",
            "    m.state,",
            "    m.create_time,",
            "    m.update_time",
            "FROM t_message m, t_user u",
            "WHERE m.post_user_id = u.id",
            "    AND m.delete_state = 0",
            "    AND m.receive_user_id = #{receiveUserId}",
            "ORDER BY m.create_time DESC",
            "LIMIT 100"
    })
    @Results({
            // 用户对象字段映射 (使用完整路径)
            @Result(property = "postUser.id", column = "u_id"),
            @Result(property = "postUser.avatarUrl", column = "u_avatar_url"),
            @Result(property = "postUser.nickname", column = "u_nickname")
    })
    List<Message> selectByReceiveUserId(Integer receiveUserId);
}
